using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace Model
{
    public partial class Tbl001_UsersInfo : DBInteractionBase
	{
		#region Class Member Declarations
			private SqlBoolean		_uLock, _uActive;
			private SqlInt32		_uGrpID, _uGrpIDOld;
			private SqlString		_uEmail, _uPhone, _password, _userName, _uAddress, _uFullName;
			private SqlGuid			_uUserID;
		#endregion


		public Tbl001_UsersInfo()
		{
			// Nothing for now.
		}


		public override bool Insert()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl001_UsersInfo_Insert]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@UUserID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _uUserID));
				cmdToExecute.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _userName));
				cmdToExecute.Parameters.Add(new SqlParameter("@Password", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _password));
				cmdToExecute.Parameters.Add(new SqlParameter("@UFullName", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _uFullName));
				cmdToExecute.Parameters.Add(new SqlParameter("@UAddress", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _uAddress));
				cmdToExecute.Parameters.Add(new SqlParameter("@UEmail", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _uEmail));
				cmdToExecute.Parameters.Add(new SqlParameter("@UPhone", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _uPhone));
				cmdToExecute.Parameters.Add(new SqlParameter("@UActive", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _uActive));
				cmdToExecute.Parameters.Add(new SqlParameter("@ULock", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _uLock));
				cmdToExecute.Parameters.Add(new SqlParameter("@UGrpID", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _uGrpID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl001_UsersInfo::Insert::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Update()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl001_UsersInfo_Update]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@UUserID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _uUserID));
				cmdToExecute.Parameters.Add(new SqlParameter("@UserName", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _userName));
				cmdToExecute.Parameters.Add(new SqlParameter("@Password", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _password));
				cmdToExecute.Parameters.Add(new SqlParameter("@UFullName", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _uFullName));
				cmdToExecute.Parameters.Add(new SqlParameter("@UAddress", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _uAddress));
				cmdToExecute.Parameters.Add(new SqlParameter("@UEmail", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _uEmail));
				cmdToExecute.Parameters.Add(new SqlParameter("@UPhone", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _uPhone));
				cmdToExecute.Parameters.Add(new SqlParameter("@UActive", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _uActive));
				cmdToExecute.Parameters.Add(new SqlParameter("@ULock", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _uLock));
				cmdToExecute.Parameters.Add(new SqlParameter("@UGrpID", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _uGrpID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl001_UsersInfo::Update::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public bool UpdateAllWUGrpIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl001_UsersInfo_UpdateAllWUGrpIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@UGrpID", SqlDbType.Int, 4, ParameterDirection.Input, false, 10, 0, "", DataRowVersion.Proposed, _uGrpID));
				cmdToExecute.Parameters.Add(new SqlParameter("@UGrpIDOld", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _uGrpIDOld));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl001_UsersInfo::UpdateAllWUGrpIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Delete()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl001_UsersInfo_Delete]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@UUserID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _uUserID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl001_UsersInfo::Delete::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override DataTable SelectOne()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl001_UsersInfo_SelectOne]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl001_UsersInfo");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@UUserID", SqlDbType.UniqueIdentifier, 16, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _uUserID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				if(toReturn.Rows.Count > 0)
				{
					_uUserID = (Guid)toReturn.Rows[0]["UUserID"];
					_userName = toReturn.Rows[0]["UserName"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["UserName"];
					_password = toReturn.Rows[0]["Password"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Password"];
					_uFullName = toReturn.Rows[0]["UFullName"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["UFullName"];
					_uAddress = toReturn.Rows[0]["UAddress"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["UAddress"];
					_uEmail = toReturn.Rows[0]["UEmail"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["UEmail"];
					_uPhone = toReturn.Rows[0]["UPhone"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["UPhone"];
					_uActive = toReturn.Rows[0]["UActive"] == System.DBNull.Value ? SqlBoolean.Null : (bool)toReturn.Rows[0]["UActive"];
					_uLock = toReturn.Rows[0]["ULock"] == System.DBNull.Value ? SqlBoolean.Null : (bool)toReturn.Rows[0]["ULock"];
					_uGrpID = toReturn.Rows[0]["UGrpID"] == System.DBNull.Value ? SqlInt32.Null : (Int32)toReturn.Rows[0]["UGrpID"];
				}
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl001_UsersInfo::SelectOne::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public override DataTable SelectAll()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl001_UsersInfo_SelectAll]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl001_UsersInfo");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl001_UsersInfo::SelectAll::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public DataTable SelectAllWUGrpIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl001_UsersInfo_SelectAllWUGrpIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl001_UsersInfo");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@UGrpID", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _uGrpID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl001_UsersInfo::SelectAllWUGrpIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		#region Class Property Declarations
		public SqlGuid UUserID
		{
			get
			{
				return _uUserID;
			}
			set
			{
				SqlGuid uUserIDTmp = (SqlGuid)value;
				if(uUserIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("UUserID", "UUserID can't be NULL");
				}
				_uUserID = value;
			}
		}


		public SqlString UserName
		{
			get
			{
				return _userName;
			}
			set
			{
				_userName = value;
			}
		}


		public SqlString Password
		{
			get
			{
				return _password;
			}
			set
			{
				_password = value;
			}
		}


		public SqlString UFullName
		{
			get
			{
				return _uFullName;
			}
			set
			{
				_uFullName = value;
			}
		}


		public SqlString UAddress
		{
			get
			{
				return _uAddress;
			}
			set
			{
				_uAddress = value;
			}
		}


		public SqlString UEmail
		{
			get
			{
				return _uEmail;
			}
			set
			{
				_uEmail = value;
			}
		}


		public SqlString UPhone
		{
			get
			{
				return _uPhone;
			}
			set
			{
				_uPhone = value;
			}
		}


		public SqlBoolean UActive
		{
			get
			{
				return _uActive;
			}
			set
			{
				_uActive = value;
			}
		}


		public SqlBoolean ULock
		{
			get
			{
				return _uLock;
			}
			set
			{
				_uLock = value;
			}
		}


		public SqlInt32 UGrpID
		{
			get
			{
				return _uGrpID;
			}
			set
			{
				_uGrpID = value;
			}
		}
		public SqlInt32 UGrpIDOld
		{
			get
			{
				return _uGrpIDOld;
			}
			set
			{
				_uGrpIDOld = value;
			}
		}
		#endregion
	}
}
